<template>
  <view>
    <my-search @myClick="gotoSearch"></my-search>
    <view class="scroll-container">
      <!-- 左侧滚动区域 -->
        <scroll-view class="left-scroll-view" scroll-y="true" :style="{height:wh+'px'}">
          <view 
          class="left-scroll-view-item"
          :class="{ active:active == index }"
           v-for="(item,index) in categorieList"
           :key="item.cat_id"
           @click="cliackLeftItem(index)"
          >
          {{ item.cat_name }}
          </view>
          <!-- <view class="left-scroll-view-item active">xxx</view> -->
        </scroll-view>
      <!-- 右侧滚动区域 -->
      <scroll-view :scroll-top="scrlloTop" class="right-scroll-view" scroll-y="true" :style="{height:wh+'px'}">
        <view class="right-scroll-view-item" v-for="item in itemList" :key="item.cat_id">
          <view class="item">
            <!-- 标题 -->
            <view class="title">
              /{{ item.cat_name }}/
            </view>
            <!-- 内容 -->
            <view class="content">
             <view hover-class="hoverClass" class="content-item" v-for="obj in item.children" :key="obj.cat_id" @click="gotoGoodsList(obj)" >
              <!-- 图片 -->
              <view class="pic">
                <image :src="obj.cat_icon" mode="heightFix"></image>
              </view>
              <!-- 名字 -->
              <view class="text">
                {{ obj.cat_name }}
              </view>
             </view>
             <!-- 如果没有子级渲染自己 -->
              <view hover-class="hoverClass"  class="content-item" v-if="!item.children" @click="gotoGoodsList(item)">
                <!-- 图片 -->
                <view class="pic">
                  <image :src="item.cat_icon" mode="heightFix"></image>
                </view>
                <!-- 名字 -->
                <view class="text">
                  {{ item.cat_name }}
                </view>
              </view>
            </view>
          </view>
        </view>
      </scroll-view>
    </view>
  </view>
</template>

<script>
  // 导入获取分类相关的接口
  import { getCategories } from '@/api/cate.js'
    import  tabbarBadge  from '@/mixins/tabbar-badge.js'
  export default {
    mixins:[tabbarBadge],
    data() {
      return {
        wh:0,
        categorieList:[],
        itemList:[],
        active:0,
        scrlloTop:0
      };
    },
    onLoad() {
      // 获得机型的参数
      const saysInfo = uni.getSystemInfoSync()
      this.wh = saysInfo.windowHeight - 65
      this.getCategories()
    },
    methods:{
      // 获取分类列表的数据
     async getCategories() {
        const { data } = await getCategories()
        if(data.meta.status !== 200) return uni.$showMsg()
        // console.log(data);
        this.categorieList = data.message
        this.itemList = this.categorieList[0].children
      },
      // 点击左侧项
      cliackLeftItem(index) {
        this.active = index
         this.itemList = this.categorieList[index].children
         this.scrlloTop = this.scrlloTop === 0 ? 1 : 0
      },
      // 点击商品跳转到商品列表
      gotoGoodsList(obj) {
        uni.navigateTo({
          url:'/subpkg/goods_list/goods_list?cid='+ obj.cat_id
        })
      },
      // 跳转到搜索页面
      gotoSearch() {
        uni.navigateTo({
          url:'/subpkg/search/search'
        })
      }
    },
    watch:{

    }
  }
</script>

<style scoped lang="scss">
.scroll-container {
  display: flex;
  background-color: #fff;
  .left-scroll-view {
    width: 120px;
    text-align: center;
    font-size: 14px;
    background-color: #f7f7f7;
    // height: 100vh;
    .left-scroll-view-item {
      width: 100%;
      height: 150rpx;
      line-height: 150rpx;
      position: relative;
      &.active {
        background-color: #fff;
        &::before {
          position: absolute;
          top: 50%;
          left: 0;
          content: ' ';
          display: block;
          width: 3px;
          height: 30px;
          transform: translateY(-50%);
          background-color: #c00000;
        }
      }
    }
  }
  .right-scroll-view {
    // height: 100vh;
    .title {
      text-align: center;
      font-weight: 700;
      margin: 30rpx;
    }
    .content {
      display: flex;
      flex-wrap: wrap;
      justify-content: start;
      .hoverClass {
      position: relative;
      &::before {
        position: absolute;
        top: 0;
        left: 0;
        content: ' ';
        width: 100%;
        height: 100%;
        background-color: #fff;
        opacity: .4;
      }
      }
      .content-item {
        display: flex;
        flex-direction: column;
        justify-content: center;
        align-items: center;
        width: 33%;
        margin-bottom: 20rpx;
        .pic {
          height: 120rpx;
          image {
            width: 100%;
            height: 100%;
          }
        }
        .text {
          font-size: 26rpx;
        }
      }
    }
  }
}
</style>
